package com.nlelpct.manage.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.nlelpct.manage.constant.SysSolrLogConstant;
import com.nlelpct.manage.constant.SystemConstant;
import com.nlelpct.manage.context.ThreadParameter;
import com.nlelpct.manage.enums.SysOperateTypeEnum;
import com.nlelpct.manage.feign.LogFeignService;
import com.nlelpct.manage.mapper.SysCompanyMapper;
import com.nlelpct.manage.mapper.SysEmployeeMapper;
import com.nlelpct.manage.model.entity.SysEmployee;
import com.nlelpct.manage.model.entity.SysSystem;
import com.nlelpct.manage.model.query.TerminalInfo;
import com.nlelpct.manage.model.vo.ESLogVo;
import com.nlelpct.manage.model.vo.EsSystemLog;
import com.nlelpct.manage.utils.GetLoginInfoUtils;
import com.nlelpct.manage.utils.IDUtils;
import com.nlelpct.manage.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author :小马
 * @date : 2023-07-24
 */
@Service
@Slf4j
public class LogBaseService {

    private final RedisTemplate<String, Object> redisTemplate;

    private final SysEmployeeMapper sysEmployeeMapper;

    private final LogFeignService logFeignService;

    private final SysCompanyMapper sysCompanyMapper;

    @Autowired
    public LogBaseService(RedisTemplate<String, Object> redisTemplate, SysEmployeeMapper sysEmployeeMapper, LogFeignService logFeignService, SysCompanyMapper sysCompanyMapper) {
        this.redisTemplate = redisTemplate;
        this.sysEmployeeMapper = sysEmployeeMapper;
        this.logFeignService = logFeignService;
        this.sysCompanyMapper = sysCompanyMapper;
    }

    /**
     * 保存操作日志
     * @param operateTypeId 操作类型Id
     * @param map 保存内容
     * @return 保存结果
     */
    public Result saveOptLog(Integer operateTypeId, Map<String, String> map) {
        EsSystemLog systemLog = new EsSystemLog();
        systemLog.setOperateTypeId(operateTypeId);
        systemLog.setSystemId(this.getSystemId());
        systemLog.setOperateTime(LocalDateTime.now().toString());
        systemLog.setOperateTimeLong(System.currentTimeMillis());
        systemLog.setType(SysSolrLogConstant.OPT_LOG);
        systemLog.setOperatorId(this.getCurrentEmployeeId());
        systemLog.setCompanyId(this.getCurrentCompanyId());
        //企业名称
        map.put(SysSolrLogConstant.COMPANY_NAME, sysCompanyMapper.selectById(this.getCurrentCompanyId()).getCompanySysName());
        //所属页面
        map.put(SysSolrLogConstant.PAGE, this.getPage());
        //相关模块
        map.put(SysSolrLogConstant.MODULE, this.getModule());
        //操作人姓名
        map.put(SysSolrLogConstant.OPERATE_NAME, sysEmployeeMapper.selectById(this.getCurrentEmployeeId()).getEmployeeName());
        //操作类型
        map.put(SysSolrLogConstant.OPERATE_TYPE, SysOperateTypeEnum.getName(operateTypeId));
        TerminalInfo terminalInfo = this.getTerminalInfo();
        //ip地址
        map.put(SysSolrLogConstant.IP_ADDRESS, terminalInfo.getIpAddress());
        //ip所属地区
        map.put(SysSolrLogConstant.IP_AREA, terminalInfo.getIpArea());
        //操作系统
        map.put(SysSolrLogConstant.OS, terminalInfo.getOs());
        //浏览器名称和内核
        map.put(SysSolrLogConstant.BROWSE, terminalInfo.getBrowser());
        systemLog.setContent(JSONObject.toJSONString(map));
        return logFeignService.saveLog(systemLog);
    }
/*
    *//**
     * 获取系统详情
     * @return 系统详情
     *//*
    private SysSystem getCurrentSystem() {
        String systemId = this.getSystemId();
        return sysSystemService.getById(systemId);
    }*/

    public Result exportOptLogByIds(List<String> ids) {

        return logFeignService.exportOptLogByIds(ids);
    }

    public Result exportAllOptLog(ESLogVo vo) {
        vo.setCompany(this.getCurrentCompanyId());
        vo.setKey(this.getCurrentEmployeeId());
        return logFeignService.exportAllOptLog(vo);
    }

    /**
     * 获取IP等信息
     */
    public TerminalInfo getTerminalInfo() {
        String jsonTerminalInfo = ThreadParameter.getLocalMap().get(SystemConstant.TERMINAL_INFO);
        return JSON.parseObject((String) redisTemplate.opsForValue().get(jsonTerminalInfo), TerminalInfo.class);
    }

    /**
     * 获取当前系统ID
     *
     * @return 系统ID
     */
    public String getSystemId() {
        return GetLoginInfoUtils.getSystemId();
    }

    /**
     * 获取员工ID
     * @return 员工ID
     */
    public String getCurrentEmployeeId() {
        return GetLoginInfoUtils.getEmployeeId();
    }

    /**
     * 获取企业ID
     * @return 企业ID
     */
    public String getCurrentCompanyId() {
        return GetLoginInfoUtils.getCompanyId();
    }

    /**
     * 获取当前员工信息
     *
     * @return 员工信息
     */
    public SysEmployee getEmployee() {
        String systemId = ThreadParameter.getLocalMap().get(SystemConstant.SYSTEM_ID);
        String employeeId = ThreadParameter.getLocalMap().get(SystemConstant.EMPLOYEE_ID);
        String jsonEmployee = (String) redisTemplate.opsForValue().get(systemId + ":" + employeeId);
        if (ObjectUtil.isEmpty((jsonEmployee))) {
            SysEmployee sysEmployee = sysEmployeeMapper.selectById(employeeId);
            redisTemplate.opsForValue().set(systemId + ":" + employeeId, JSON.toJSONString(sysEmployee), 1L, TimeUnit.HOURS);
            return sysEmployee;
        }
        return JSON.parseObject(jsonEmployee, SysEmployee.class);
    }

    public String getSysTemLogId() {
        return ThreadParameter.getLocalMap().get(SystemConstant.LOG_BASE_INFO_REDIS_KEY);
    }

    public String getModule() {
        return ThreadParameter.getLocalMap().get(SystemConstant.MODULE);
    }

    public String getPage() {
        return ThreadParameter.getLocalMap().get(SystemConstant.PAGE);
    }


}
